package servlet.rool;

import net.sf.json.JSONObject;
import pojo.Comcollect;
import pojo.User;
import service.ComcollectService;
import service.OrderService;
import service.impl.ComcollectServiceImpl;
import service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 前台收藏商品的servlet
 */
@WebServlet(value = "/CollectionServlet")
public class CollectionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        OrderService os=new OrderServiceImpl();
        User user=os.userName(name);
        ComcollectService cs=new ComcollectServiceImpl();
        Comcollect comcollect=new Comcollect();
        comcollect.setUId(user.getuID());
        comcollect.setWareId(Integer.parseInt(id));
        int bo=cs.addComcollect(comcollect);
        JSONObject json=new JSONObject();
        if(bo>0){
            json.put("msg","收藏成功");
        }else {
            json.put("msg","收藏失败");
        }
        PrintWriter out = response.getWriter();
        out.print(json);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        doGet(request, response);
    }
}
